<template>
  <div id="app">
    <!-- keep-alive作用：对组件进行缓存，从而节省性能 -->
    <keep-alive :exclude="excludeName">
      <transition :name="transitionName">
        <router-view />
      </transition>
    </keep-alive>
  </div>
</template>
<script>
export default {
  name: "app",
  beforeCreate() {
    const token = sessionStorage.getItem("token");
    if (!this.$Global.isNull(token)) {
      this.$store.commit("SET_TOKEN", token);
    }
    const userInfo = sessionStorage.getItem("userInfo");
    if (!this.$Global.isNull(userInfo)) {
      this.$store.commit("SET_USERINFO", JSON.parse(userInfo));
    }
  },
  created() {
    //在页面加载时读取sessionStorage里的状态信息
    if (sessionStorage.getItem("store")) {
      this.$store.replaceState(
        Object.assign(
          {},
          this.$store.state,
          JSON.parse(sessionStorage.getItem("store"))
        )
      );
    }
    //在页面刷新时将vuex里的信息保存到sessionStorage里
    window.addEventListener("beforeunload", () => {
      sessionStorage.setItem("store", JSON.stringify(this.$store.state));
    });
  },
  watch: {
    $route(to, from) {
      const toDepth = to.path.split("/").length;
      const fromDepth = from.path.split("/").length;
      this.transitionName = toDepth < fromDepth ? "slide-right" : "slide-left";
    },
  },
};
</script>
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  margin: 0px;
  padding: 0px;
  width: 100%;
  height: 100%;
  /* text-align: center; */
  /* color: #2c3e50; */
}
</style>
